package editorview.views;

import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.PartInitException;

public abstract class IEditorReferenceLabelProvider extends StyledCellLabelProvider {

  @Override
  public final void update(ViewerCell cell) {

    Object element = cell.getElement();
    if (element instanceof IEditorReference) {
      IEditorReference editorReference = (IEditorReference) element;
      update(cell, editorReference);
    } else {
      cell.setText("Unknown element");
    }
    super.update(cell);

    super.update(cell);
  }

  public abstract void update(ViewerCell cell, IEditorReference editorReference);

  public IPathEditorInput getPathEditorInput(IEditorReference editorReference) {
    try {
      IEditorInput editorInput = editorReference.getEditorInput();
      if (editorInput instanceof IPathEditorInput) {
        return (IPathEditorInput) editorInput;
      }
    } catch (PartInitException e) {
      throw new IllegalStateException("Can't get path editor input.", e);
    }
    throw new IllegalStateException("Can't get path editor input.");
  }
}
